/*
 * 项目名称:项目名称
 * 类名称:MldAppointmentServiceImpl.java
 * 包名称:com.tongyi.modules.mld.service.impl
 * @author 惠州市酷天科技有限公司
 * @date 2024-10-28 21:04:48
 * Copyright (c) 2019-2021 惠州市酷天科技有限公司
 */
package com.tongyi.modules.mld.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.CaseFormat;
import com.google.common.collect.Sets;
import com.tongyi.common.exception.BusinessException;
import com.tongyi.common.utils.ShiroUtils;
import com.tongyi.common.utils.TokenGenerator;
import com.tongyi.core.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tongyi.common.utils.Query;
import com.tongyi.modules.mld.dao.MldAppointmentDao;
import com.tongyi.modules.mld.entity.MldAppointmentEntity;
import com.tongyi.modules.mld.entity.MldCustomBaseEntity;
import com.tongyi.modules.mld.entity.MldCustomReservationEntity;
import com.tongyi.modules.mld.service.MldAppointmentService;
import com.tongyi.modules.mld.service.MldCustomBaseService;
import com.tongyi.modules.sys.entity.SysUserEntity;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.io.Serializable;

/**
 * Service实现类
 *
 * @author 惠州市酷天科技有限公司
 * @date 2024-10-28 21:04:48
 */
@Service("mldAppointmentService")
public class MldAppointmentServiceImpl extends ServiceImpl<MldAppointmentDao, MldAppointmentEntity> implements MldAppointmentService{

    @Autowired
    private MldCustomBaseService mldCustomBaseService;

    @Override
    public MldAppointmentEntity getById(Serializable id){
        return super.getById(id);
    }

    @Override
    public List<MldAppointmentEntity> listAll(Map<String, Object> params) {
        return super.baseMapper.listAll(params);
    }

    @Override
    public PageInfo<MldAppointmentEntity> listPage(int current, int size,Map<String, Object> params) {
        params.remove("page");
        params.remove("limit");
        Page<MldAppointmentEntity> page = new Page<>(current, size);
        QueryWrapper<MldAppointmentEntity> wrapper = new QueryWrapper<>();
        if(MapUtils.isNotEmpty(params)){
            Set<String> excludeKey = Sets.newHashSet("createTime[]","appointmentTime[]","createTimeStart","createTimeEnd","appointmentTimeStart","appointmentTimeEnd");
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if(CollectionUtils.containsAny(excludeKey,entry.getKey())){
                   continue;
                }
                wrapper.eq(Objects.nonNull(entry.getValue()) && !"".equals(entry.getValue()), CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entry.getKey()), entry.getValue());
            }
            wrapper.and(params.containsKey("createTime[]"),m -> m.between("create_time",params.get("createTimeStart"),params.get("createTimeEnd")))
                    .and(params.containsKey("appointmentTime[]"),m->m.between("appointment_time",params.get("appointmentTimeStart"),params.get("appointmentTimeEnd")))
                    .orderByAsc("id");
        }


        List<MldAppointmentEntity> mldAppointmentEntities = super.baseMapper.selectPage(page, wrapper).getRecords();
        page.setRecords(mldAppointmentEntities);
        return new PageInfo<MldAppointmentEntity>(page.getCurrent(), page.getSize(), page.getTotal()).setList(page.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addEntity(MldAppointmentEntity entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEntity(MldAppointmentEntity entity) {
        return super.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEntity(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Serializable[] ids) {
        return super.removeByIds(Arrays.asList(ids));
    }

    @Override
    public void update(MldAppointmentEntity entity) {
        if(StringUtils.isNotBlank(entity.getUserId())){
            MldCustomBaseEntity mldCustomBaseEntity = mldCustomBaseService.queryByUserId(entity.getUserId());
            if(Objects.isNull(mldCustomBaseEntity)){
                throw new BusinessException("客户id不存在");
            }
        }
        SysUserEntity sysUserEntity = ShiroUtils.getUserEntity();
        if (Objects.isNull(sysUserEntity)) {
            throw new BusinessException("用户登录过期或者未登录");
        }
        entity.setAppointmentEmployeeId(sysUserEntity.getUserId());
        entity.setDepartment(sysUserEntity.getOrgNo());
        entity.setUpdateTime(new Date());
        this.updateEntity(entity);
    }

    @Override
    public void add(MldAppointmentEntity entity) throws Exception {
        SysUserEntity sysUserEntity = ShiroUtils.getUserEntity();
        if (Objects.isNull(sysUserEntity)) {
            throw new BusinessException("用户登录过期或者未登录");
        }
        entity.setAppointmentEmployeeId(sysUserEntity.getUserId());
        entity.setDepartment(sysUserEntity.getOrgNo());
        MldCustomBaseEntity mldCustomBaseEntity = mldCustomBaseService.queryByUserId(entity.getUserId());
        if(Objects.isNull(mldCustomBaseEntity)){
            throw new BusinessException("客户id不存在");
        }

        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setAppointmentId(TokenGenerator.generateValue());
        this.addEntity(entity);
    }
}
